package com.gigi.service.manual;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gigi.entity.FollowShiqinEntity;
import com.gigi.entity.UserShiqinEntity;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.enums.YesOrNoEnum;
import com.gigi.mapper.generated.FollowShiqinMapper;
import com.gigi.model.FollowDO;
import com.gigi.model.ListFollowRequest;
import com.gigi.service.generated.FollowShiqinService;
import com.gigi.service.generated.UserShiqinService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class FollowManageService {

    private final FollowShiqinMapper followShiqinMapper;
    private final FollowShiqinService followShiqinService;
    private final UserShiqinService userShiqinService;

    public FollowManageService(FollowShiqinMapper followShiqinMapper, FollowShiqinService followShiqinService,
                               UserShiqinService userShiqinService) {
        this.followShiqinMapper = followShiqinMapper;
        this.followShiqinService = followShiqinService;
        this.userShiqinService = userShiqinService;
    }

    /**
     * 列表接口
     *
     * @param request
     * @return
     */
    public MultiResponse<FollowDO> list(ListFollowRequest request) {
        if (request.getPageNum() < 1) {
            request.setPageNum(1);
        }
        if (request.getPageSize() < 1) {
            request.setPageSize(10);
        }

        LambdaQueryWrapper<FollowShiqinEntity> wrapper = new LambdaQueryWrapper<FollowShiqinEntity>()
                .eq(FollowShiqinEntity::getUserId, request.getUserId())
                .orderByDesc(FollowShiqinEntity::getId);

        List<FollowShiqinEntity> list;
        int total = 0;

        if (YesOrNoEnum.YES.getCode().equals(request.getIfByPage())) {
            Page<FollowShiqinEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
            Page<FollowShiqinEntity> followPage = followShiqinMapper.selectPage(page, wrapper);
            list = followPage.getRecords();
            total = (int) followPage.getTotal();

        } else {
            list = followShiqinService.list(wrapper);
        }

        if (CollUtil.isEmpty(list)) {
            return MultiResponse.of(Collections.emptyList(), 0);
        }

        List<FollowDO> followDOS = list.stream().map(e -> {
            UserShiqinEntity user = userShiqinService.getById(e.getFollowUserId());
            FollowDO followDO = new FollowDO();
            followDO.setId(e.getId());
            followDO.setUserId(e.getUserId());
            followDO.setFollowUserId(e.getFollowUserId());
            if (Objects.nonNull(user)) {
                followDO.setUserName(user.getName());
                followDO.setUserPic(user.getPic());
                followDO.setUserSign(user.getSign());
            }
            return followDO;
        }).collect(Collectors.toList());
        return MultiResponse.of(followDOS, total);
    }

    /**
     * 新增/删除接口
     *
     * @param request
     * @return
     */
    public Response change(FollowDO request) {
        if (Objects.isNull(request.getId())) {
            FollowShiqinEntity entity = new FollowShiqinEntity();
            entity.setUserId(request.getUserId());
            entity.setFollowUserId(request.getFollowUserId());
            if (!followShiqinService.save(entity)) {
                return Response.buildFailure(ErrorCodeEnum.SAVE_FAILED.getErrCode(), ErrorCodeEnum.SAVE_FAILED.getErrDesc());
            }
        } else {
            if (!followShiqinService.removeById(request.getId())) {
                return Response.buildFailure(ErrorCodeEnum.DELETE_FAILED.getErrCode(), ErrorCodeEnum.DELETE_FAILED.getErrDesc());
            }
        }
        return Response.buildSuccess();
    }

}
